package fr.teuteuf.runstickmanrun;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;

public class Main implements ActionListener, KeyListener {
	private JTextField jTextField = new JTextField();
	private JFrame jFrame;
	
	public static void main(String[] args) {
		new Main();
	}
	
	public Main() {
		jFrame = new JFrame("Username:");
		jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jFrame.setLocationRelativeTo(null);
		jFrame.setSize(300, 120);
		jFrame.add(new JLabel("Please input your username:"), BorderLayout.NORTH);
		jFrame.add(jTextField);
		JButton jButton = new JButton("Start the game!");
		jButton.addActionListener(this);
		jFrame.add(jButton, BorderLayout.SOUTH);
		jFrame.setVisible(true);
		jTextField.addKeyListener(this);
		
		if(Constants.IS_DEBUG_MODE) {
			jTextField.setText("TeuTeuf");
			actionPerformed(null);
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(jTextField.getText().length() > 0) {
			jFrame.setVisible(false);
			LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
			cfg.title = "Run, Stickman, Run!";
			cfg.useGL20 = false;
			cfg.width = 1280;
			cfg.height = 720;
			
			//Fix tearing problem
			cfg.useCPUSynch = false;
			cfg.vSyncEnabled = true;
			
			new LwjglApplication(new RunStickmanRun(new DesktopKeyMap(), false, jTextField.getText()+" (PC)"), cfg);
		}
	}

	@Override
	public void keyPressed(KeyEvent e) {
		if(e.getKeyCode() == KeyEvent.VK_ENTER) {
			actionPerformed(null);
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {
	}

	@Override
	public void keyTyped(KeyEvent e) {
	}
}
